/*
 * Month.java   1.0 Nov 6, 2013
 */


public enum Month {
    DUMMY("", 0, 0),
    JANUARY("January", 1, 31),
    FEBRUARY("February", 2, 28),
    MARCH("March", 3, 31),
    APRIL("April", 4, 30),
    MAY("May", 5, 31),
    JUNE("June", 6, 30),
    JULY("July", 7, 31),
    AUGUST("August", 8, 31),
    SEPTEMBER("September", 9, 30),
    OCTOBER("October", 10, 31),
    NOVEMBER("November", 11, 30),
    DECEMBER("December", 12, 31);

    private int dayCount;
    private String name;
    private int order;

    Month(String name, int order, int dayCount) {
        this.dayCount = dayCount; 
        this.name = name;
        this.order = order;
    }

    public static boolean isLeapYear(int year) {
        boolean isLeap = false;

        if ((year % 400 == 0)
                || (year % 100 == 0)
                || (year % 4 == 0)) {
            isLeap = true;
        }

        return isLeap;
    }

    public int getDayCount(int year) {
        final int LEAP_YEAR_FEBRUARY_DAYS = 29;

        if ((order == 2) && Month.isLeapYear(year)) {
            return LEAP_YEAR_FEBRUARY_DAYS;
        }

        return dayCount;
    }

    public int getOrder() {
        return order;
    }

    public static Month getValueByName(String name) {
        for (Month month : values()) {
            if (month.order < 1) {
                continue;
            }

            String monthName = month.name.toLowerCase();
            boolean isMonthNameOrShortnameEqual
                = monthName.equals(name.toLowerCase())
                        || monthName.substring(0,3).equals(name.toLowerCase());

            if (isMonthNameOrShortnameEqual) {
                return month;
            }
        }

        return DUMMY;
    }
}
